package com.nbb.controller;

import com.google.common.collect.Maps;
import com.nbb.bean.Agent;
import com.nbb.bean.Article;
import com.nbb.itf.AgentService;
import com.nbb.itf.ArticleService;
import common.util.PropertiesUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by sagagyq on 2017/4/1.
 */
@Controller
@Log4j
@RequestMapping("/pub")
public class FirstTestController {

    @Resource
    private ArticleService articleService;


    @Resource
    private AgentService agentService;

    @Value("#{configGroup}")
    private Map<String, String> configGroup;

    @Value("#{configGroup['username']}")
    private String username;

    /**
     * zk配置测试3
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectZkTest3")
    public Object selectZkTest3(){
        Map map = Maps.newHashMap();
        map.put("username",username);
        return map;
    }

    /**
     * zk配置测试2
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectZkTest2")
    public Object selectZkTest2(){
        String username = configGroup.get("username");
        Map map = Maps.newHashMap();
        map.put("username",username);
        return map;
    }

    @ResponseBody
    @RequestMapping("/tt")
    public Object tt(){
        Map resultMap = Maps.newHashMap();
        resultMap.put("name","tom");
        resultMap.put("age",11);
        return resultMap;
    }

    /**
     * 获取部署环境
     * @return
     */
    @ResponseBody
    @RequestMapping("/getEnvironment")
    public Object getEnvironment(){
        Map resultMap = Maps.newHashMap();
        String environment = PropertiesUtil.getString("environment");
        resultMap.put("environment",environment);
        return resultMap;
    }

    /**
     * 根据id获取文章
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getArticle")
    public Object getArticle(int id){
        Article article = articleService.selectArticle(id);
        return article;
    }


    /**
     * 根据id获取用户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAgent")
    public Object getAgent(int id){
        Agent agent =  agentService.selectAgent(id);
        return agent;
    }


    /**
     * zk配置测试
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectZkTest")
    public Object selectZkTest(){
       return agentService.selectZkTest();
    }

}
